Page({
  data: {
    // tab切换
    currentTab: 0,
    activeid: 0,
    kind: 1,
    icon1: "",
    calendarConfig: {
      markToday: '今',
    },
    switch1: 0,
    dle_goods_list:[],
    icon_arr:[],
    currentDate: null,
    isEdit: true
  },
  onLoad: function() {
    var that = this;
    getApp().request({
      url: getApp().api.default.browse_list,
      data: {
        kind:that.data.kind
      },
      success: function(t) {
        if(t.code == 0 ){
          that.setData({
            commodity: t.data.re_arr,
            Store: t.data.re_shop_arr
          });
          // 可优化
          for (var y in that.data.commodity){
            that.data.commodity[y].icon = ""
          };
          that.setData({
            commodity: that.data.commodity
          })
        }
      }
    });


  },

  //日期选择
  chooseDate:function(){
    var that = this;
    var choose_day = this.calendar.getSelectedDay();
    var day_info = choose_day[0].year + '-' +  choose_day[0].month + '-' + choose_day[0].day;

    getApp().request({
      url: getApp().api.default.browse_list,
      data: {
          day: day_info,
          kind: that.data.kind
      },
      success: function(t) {
        if(t.code == 0 ){
          that.setData({
            commodity: t.data.re_arr,
            Store: t.data.re_shop_arr,
            currentDate: day_info
          })
        }
      }
    });
  },

  // 商品导航
  navigatorGoods:function(e){
    getApp().core.navigateTo({
      url: '/pages/goods/goods?id=' + e.currentTarget.dataset.id
    });
  },

  //门店导航
  navigatorShop:function(e){
    getApp().core.navigateTo({
      url: '/mch/shop/shop?mch_id=' + e.currentTarget.dataset.id
    });
  },

  //删除商品浏览记录
  dleGoodsLog:function(e){
    var that = this;
    getApp().request({
      url: getApp().api.default.dle_goods_log,
      data: {
          id: e.currentTarget.dataset.id
      },
      success: function(t) {
        if(t.code == 0){
          getApp().core.showToast({
            title: t.msg,
            icon: 'none',
            duration:1000,
            complete: function(){
              that.onLoad();
            }
          });
        }
      }
    });
  },

  //删除门店浏览记录
  dleShopLog:function(e){
    var that = this;
    getApp().request({
      url: getApp().api.default.dle_shop_log,
      data: {
          id: e.currentTarget.dataset.id
      },
      success: function(t) {
        if(t.code == 0){
          getApp().core.showToast({
            title: t.msg,
            icon: 'none',
            duration:1000,
            complete: function(){
              that.onLoad();
            }
          });
        }
      }
    });
  },

  //  tab切换逻辑
  swichNav: function(e) {
    var that = this;
    that.setData({
      activeid: 0,
      dle_goods_list:[],
      icon1: '',
      isEdit: true
    })
    if (this.data.currentTab === e.target.dataset.current) {
      return false;
    } else {
      that.setData({
        currentTab: e.target.dataset.current,
        kind: e.target.dataset.current
      },function (){
        if(e.target.dataset.current != 0){
          that.onLoad();
        }
      })
      
    }
  },
  // 编辑足迹
  compile() {
    this.setData({
      activeid: 1,
      dle_goods_list:[],
      icon1: '',
      isEdit: false
    })
  },
  cancel() {
    this.setData({
      activeid: 0,
      dle_goods_list:[],
      isEdit: true
    })
  },
  del() {
    getApp().request({
      url: getApp().api.default.dle_goods_log,
      data: {
          list: this.data.dle_goods_list.join(",")
      },
      success: function(t) {
        if(t.code == 0){
          getApp().core.showToast({
            title: t.msg,
            icon: 'none',
            duration:1000,
            // complete: function(){
            //   getApp().core.navigateTo({
            //     url: '/mch/browse/browse'
            //   });
            // }
          });
        }
      }
    });
    this.setData({
      activeid: 0,
      dle_goods_list:[]
    })
  },
  click1() {
    var that = this;
    if (!this.data.icon1) {
      for (var y in that.data.commodity){
        that.data.commodity[y].icon = "../images/browse/on.png"
      };
      that.setData({
        commodity: that.data.commodity,
        icon1:"../images/browse/on.png"
      }) 
    } else {
      for (var y in that.data.commodity){
        that.data.commodity[y].icon = ""
      };
      that.setData({
        commodity: that.data.commodity,
        icon1:""
      }) 
    }
  },

  //添加删除商品记录
  add_dle(e){
    var s = this;
    var add_goods_id = e.currentTarget.dataset.id;
    var icon_index = e.currentTarget.dataset.index;
    var icon1 = '';
    if(s.data.commodity[icon_index].icon){
      //删除
      for(var i in s.data.dle_goods_list){
        if(s.data.dle_goods_list[i] == add_goods_id){
          s.data.dle_goods_list.splice(i,1);
        }
      }
      s.data.commodity[icon_index].icon = ""; 
      icon1 = '';
    }else{
      //增加
      s.data.dle_goods_list.push(add_goods_id);
      s.data.commodity[icon_index].icon = "../images/browse/on.png"; 
      if(s.data.dle_goods_list.length == s.data.commodity.length){
        icon1="../images/browse/on.png"
      }
    }
    s.setData({
      commodity: s.data.commodity,
      icon1: icon1
    });
  },

  switchView() {
    if (this.switch1 == 1) {
      this.calendar.switchView('week').then(() => {});
      this.switch1 = 2;
    } else {
      this.calendar.switchView('month').then(() => {});
      this.switch1 = 1;
    }
  },
})